# -*- coding: utf-8 -*-
import urllib2
import MySQLdb as mysql
import time
import datetime
from core import lock_task, add_task, del_task, check_valid
from var import *
from download import download
import var

class CheckDbThread(threading.Thread):
    def __init__(self,q):
        threading.Thread.__init__(self)
        self.queue = q
        self.conn = mysql.connect(host=db_host,user=db_user,passwd=db_passwd,db=main_db,charset='utf8')
        self.cursor = self.conn.cursor()
    def run(self):
        while True:
            job = self.queue.get()
            self.checkDb(job)
            self.queue.task_done()
    def checkDb(self,job):
        self.cursor.execute("select count(*) from scannedIDs where articleID=%s",job[0])
        cnt = self.cursor.fetchall()[0][0]
        if cnt:
            return
        add_task(job[0],job[1])
        q_filter.put(job[0])
        self.cursor.execute("insert into scannedids values(%s,%s,%s)", (None,job[0],job[1]))

class FilterThread(threading.Thread):
    def __init__(self,q):
        threading.Thread.__init__(self)
        self.queue = q
    def run(self):
        while True:
            job = self.queue.get()
            self.filter_work(job)
            self.queue.task_done()
    def filter_work(self,job):
        result = check_valid(job)
        if result == 0:
            del_task(job)
        if result == 1:
            lock_task(job)
        if result == -1:
            q_filter.put(job)

class DownloadThread(threading.Thread):
    def __init__(self,q):
        threading.Thread.__init__(self)
        self.queue = q
    def run(self):
        while True:
            job = self.queue.get()
            self.download_work(job)
            self.queue.task_done()
    def download_work(self,job):
        try:
            download(job)
        except urllib2.HTTPError:
            if job.dbtype == '2' or job.dbtype == '1':
                del_task(job.id)
        #except Exception,e:
        #   print e,job.name,'haha'

def start_threads(*threads_to_enabled):
    for i in range(FilterThreadNumber):  # 1
        t = FilterThread(q_filter)
        t.setDaemon(True)
        t.start()
    for i in range(CheckDbThreadNumber):  # 2
        t = CheckDbThread(q_db)
        t.setDaemon(True)
        t.start()
    # 开启下载线程
    for i in range(DownloadThreadNumber):
        t = DownloadThread(download_queue)
        t.setDaemon(True)
        t.start()
